const httpclient = require('../../utils/httpclient.js')
//获取应用实例
var app = getApp()
var EARTH_RADIUS = 6378137.0;    //单位M
var PI = Math.PI;
Page({
  data: {
    addressList:[]
  },

  selectTap: function (e) {
    var id = e.currentTarget.dataset.id;
    // 设置该ID的收货信息为默认地址
    httpclient.req(
      "/mall/set_default_consignee",
      {
        user_id: app.globalData.userInfo.id,
        id: id
      },
      'post',
      function (res) {
        if(res.data.result){

          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];  //上一个页面

          //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            curAddressData: res.data.consignee
          })
          wx.navigateBack({
            
          });
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },

  addAddess : function () {
    wx.navigateTo({
      url:"/pages/address-add/index"
    })
  },

  delAddess:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          // 设置该ID的收货信息为默认地址
          httpclient.req(
            "/mall/del_consignee_one",
            {
              id: id
            },
            'post',
            function (res) {
              if (res.data.result) {
                that.initShippingAddress();
              }
            },
            function (result) {
              console.log(result)
            }
          );
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  editAddess: function (e) {
    wx.navigateTo({
      url: "/pages/address-add/index?id=" + e.currentTarget.dataset.id
    })
  },
  
  onLoad: function () {
    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    // 设置店铺的经纬度
    that.setData({
      longitude: 106.4782100000,
      latitude: 29.5565300000
    });
  },
  onShow : function () {
    this.initShippingAddress();
  },
  initShippingAddress: function () {
    var that = this;
    httpclient.req(
      "/mall/query_user_consignee_info",
      {
        user_id: app.globalData.userInfo.id
      },
      'post',
      function (res) {
        var addressList = res.data;
        console.log(addressList);
        for (var i = 0; i < addressList.length;i++){
          addressList[i].distance = that.getFlatternDistance(that.data.latitude, that.data.longitude, addressList[i].latitude,
            addressList[i].longitude);
        }

        that.setData({
          addressList: addressList
        });
      },
      function (result) {
        console.log(result)
      }
    );
  },
  /**
     * approx distance between two points on earth ellipsoid
     * @param {Object} lat1
     * @param {Object} lng1
     * @param {Object} lat2
     * @param {Object} lng2
     */
  getFlatternDistance:function(lat1, lng1, lat2, lng2){
    var f = this.getRad((lat1 + lat2) / 2);
    var g = this.getRad((lat1 - lat2) / 2);
    var l = this.getRad((lng1 - lng2) / 2);
        
    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);
        
    var s, c, w, r, d, h1, h2;
    var a = EARTH_RADIUS;
    var fl = 1 / 298.257;
        
    sg = sg * sg;
    sl = sl * sl;
    sf = sf * sf;
        
    s = sg * (1 - sl) + (1 - sf) * sl;
    c = (1 - sg) * (1 - sl) + sf * sl;
        
    w = Math.atan(Math.sqrt(s / c));
    r = Math.sqrt(s * c) / w;
    d = 2 * w * a;
    h1 = (3 * r - 1) / 2 / c;
    h2 = (3 * r + 1) / 2 / s;
        
    return d* (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
  },
  getRad:function (d){
    return d* PI / 180.0;
  }

})
